from django.http import JsonResponse
from django.shortcuts import render
from django.views.decorators.csrf import csrf_exempt
from django.contrib import auth
from django.shortcuts import render, HttpResponse
from LoginManage.models import *
from LoginManage.validCode import *
from PersonManage.models import *


# Create your views here.
@csrf_exempt
def get_validCode_img(request):
    """
    基于PIL模块动态生成响应状态码图片
    :param request:
    :return:
    """
    data = get_valid_code_img(request)
    return HttpResponse(data)

@csrf_exempt
def employeeRegister(request):
    if request.method == 'POST':
        print(request.POST.get)
        id = request.POST.get('WorkID')
        passwd = request.POST.get('passwd')
        confirmPwd = request.POST.get('confirmPwd')
        count1 = Staff.objects.filter(id=id).count()
        count2 = Driver.objects.filter(id=id).count()
        if count1 == 0 and count2 == 0:
            return JsonResponse({"Message": "you are not the driver or staff"}, safe=False)
        else:
            if count2 != 0:
                try:
                    Driver.objects.get(id=id, isRegister=True)
                except Driver.DoesNotExist:
                    if confirmPwd != passwd:
                        return JsonResponse({"Message": "two passwd is not equal"}, safe=False)
                    else:
                        driver = Driver.objects.get(id=id)
                        driver.id = id
                        driver.passwd = passwd
                        driver.isRegister = True
                        driver.save()
                        return JsonResponse({"Message": "driver register success"}, safe=False)
                return JsonResponse({"Message": "this driver has registered already"}, safe=False)
            elif count1 != 0:
                try:
                    Staff.objects.get(id=id, isRegister=True)
                except Staff.DoesNotExist:
                    if confirmPwd != passwd:
                        return JsonResponse({"Message": "two passwd is not equal"}, safe=False)
                    else:
                        staff = Staff.objects.get(id=id)
                        staff.id = id
                        staff.passwd = passwd
                        staff.isRegister = True
                        staff.save()
                        return JsonResponse({"Message": "staff register success"}, safe=False)
                return JsonResponse({"Message": "this staff has registered already"}, safe=False)

@csrf_exempt
def userRegister(request):
    if request.method == 'POST':
        print(request.POST.get)
        Username = request.POST.get('Username')
        count = Customer.objects.filter(Username=Username).count()
        if count == 0:
            email = request.POST.get('email')
            passwd = request.POST.get('passwd')
            confirmPwd = request.POST.get('confirmPwd')
            if confirmPwd != passwd:
                return JsonResponse({"Message": "two passwd is not equal"}, safe=False)
            else:
                newCu = Customer()
                newCu.Username = Username
                newCu.passwd = passwd
                newCu.email = email
                newCu.save()
                return JsonResponse({"Message": "user register success"}, safe=False)
        else:
            return JsonResponse({"Message": "this username is already registered"}, safe=False)


@csrf_exempt
def login(request):
    if request.method == 'POST':
        print(request.POST.get)
        id = request.POST.get('id')
        passwd = request.POST.get('passwd')
        countd = Driver.objects.filter(id=id).count()
        counts = Staff.objects.filter(id=id).count()
        countu = Customer.objects.filter(Username=id).count()
        if countd == 0 and counts == 0 and countu == 0:
            return JsonResponse({"Message": "this id or username is not existed"}, safe=False)
        elif countd != 0:
            driver = Driver.objects.get(id=id)
            if driver.passwd == passwd:
                driver.isLogin = True
                driver.save()
                return JsonResponse({"Message": "司机 login success"}, safe=False)
            else:
                return JsonResponse({"Message": "passwd wrong"}, safe=False)
        elif counts != 0:
            staff = Staff.objects.get(id=id)
            if staff.passwd == passwd:
                staff.isLogin = True
                staff.save()
                if staff.id.startswith("HR"):
                    return JsonResponse({"Message": "人事管理 login success"}, safe=False)
                if staff.id.startswith("CM"):
                    return JsonResponse({"Message": "车辆管理 login success"}, safe=False)
                if staff.id.startswith("CS"):
                    return JsonResponse({"Message": "客服 login success"}, safe=False)
                if staff.id.startswith("RM"):
                    return JsonResponse({"Message": "路线管理 login success"}, safe=False)
            else:
                return JsonResponse({"Message": "passwd wrong"}, safe=False)
        elif countu != 0:
            user = Customer.objects.get(Username=id)
            if user.passwd == passwd:
                return JsonResponse({"Message": "客户 login success"}, safe=False)
            else:
                return JsonResponse({"Message": "passwd wrong"}, safe=False)


@csrf_exempt
def changePwd(request):
    if request.method == 'POST':
        print(request.POST.get)
        id = request.POST.get('id')
        oldpasswd = request.POST.get('oldpasswd')
        newpasswd = request.POST.get('passwd')
        newpasswd1 = request.POST.get('passwd1')
        countd = Driver.objects.filter(id=id).count()
        counts = Staff.objects.filter(id=id).count()
        countu = Customer.objects.filter(Username=id).count()
        if countd != 0:
            driver = Driver.objects.get(id=id)
            if driver.passwd == oldpasswd:
                if newpasswd == newpasswd1:
                    driver.passwd = newpasswd
                    driver.save()
                    return JsonResponse({"Message": "driver change passwd success"}, safe=False)
                else:
                    return JsonResponse({"Message": "two passwd is not equal"}, safe=False)
            else:
                return JsonResponse({"Message": "oldPasswd is wrong"}, safe=False)
        elif counts != 0:
            staff = Staff.objects.get(id=id)
            if staff.passwd == oldpasswd:
                if newpasswd == newpasswd1:
                    staff.passwd = newpasswd
                    staff.save()
                    if staff.id.startswith("HR"):
                        return JsonResponse({"Message": "人事管理 change passwd success"}, safe=False)
                    elif staff.id.startswith("CM"):
                        return JsonResponse({"Message": "车辆管理 change passwd success"}, safe=False)
                    elif staff.id.startswith("CS"):
                        return JsonResponse({"Message": "客服 change passwd success"}, safe=False)
                    elif staff.id.startswith("RM"):
                        return JsonResponse({"Message": "路线管理 change passwd success"}, safe=False)
                else:
                    return JsonResponse({"Message": "two passwd is not equal"}, safe=False)
            else:
                return JsonResponse({"Message": "oldPasswd is wrong"}, safe=False)
        elif countu != 0:
            user = Customer.objects.get(Username=id)
            if user.passwd == oldpasswd:
                if newpasswd == newpasswd1:
                    user.passwd = newpasswd
                    user.save()
                    return JsonResponse({"Message": "user change passwd success"}, safe=False)
                else:
                    return JsonResponse({"Message": "two passwd is not equal"}, safe=False)
            else:
                return JsonResponse({"Message": "oldPasswd is wrong"}, safe=False)

@csrf_exempt
def getNow(request):
    if request.method == 'GET':
        print('GET')
        nowLogins = []
        nowDriver_list = Driver.objects.filter(isLogin=True)
        for nowDriver in nowDriver_list:
            nowLogins.append(nowDriver.to_dict())
        nowStaff_List = Staff.objects.filter(isLogin=True)
        for nowStaff in nowStaff_List:
            nowLogins.append(nowStaff.to_dict3())
        return JsonResponse({"nowLogin": nowLogins}, safe=False)


def logout(request):
    if request.method == 'GET':
        id = request.GET.get('id')
        countd = Driver.objects.filter(id=id).count()
        counts = Staff.objects.filter(id=id).count()
        countu = Customer.objects.filter(Username=id).count()
        if countd != 0:
            driver = Driver.objects.get(id=id)
            driver.isLogin = False
            driver.save()
            return JsonResponse({"Message": "driver logout success"}, safe=False)
        elif counts != 0:
            staff = Staff.objects.get(id=id)
            staff.isLogin = False
            staff.save()
            if staff.id.startswith("HR"):
                return JsonResponse({"Message": "人事管理 logout success"}, safe=False)
            if staff.id.startswith("CM"):
                return JsonResponse({"Message": "车辆管理 logout success"}, safe=False)
            if staff.id.startswith("CS"):
                return JsonResponse({"Message": "客服 logout success"}, safe=False)
            if staff.id.startswith("RM"):
                return JsonResponse({"Message": "路线管理 logout success"}, safe=False)
        elif countu != 0:
            user = Customer.objects.get(Username=id)
            return JsonResponse({"Message": "user logout success"}, safe=False)

@csrf_exempt
def addStaffInfo(request):
    if request.method == 'POST':
        print(request.POST.get)
        id = request.POST.get('id')
        name = request.POST.get('name')
        age = request.POST.get('age')
        sex = request.POST.get('sex')
        staff = Staff.objects.get(id=id)
        staff.name = name
        staff.age = age
        staff.sex = sex
        staff.save()
        return JsonResponse({"Message": "add staff message success"}, safe=False)


@csrf_exempt
def checkStaffInfo(request):
    if request.method == 'GET':
        print('GET')
        id = request.GET.get('id')
        staff = Staff.objects.get(id=id)
        return JsonResponse({"Info": staff.to_dict7()}, safe=False)

@csrf_exempt
def checkUserInfo(request):
    if request.method == 'GET':
        print('GET')
        username = request.GET.get('Username')
        user = Customer.objects.get(Username=username)
        return JsonResponse({"Info": user.to_dict10()}, safe=False)